﻿using System;
using System.Collections.ObjectModel;
using System.Linq;
using GalaSoft.MvvmLight.Command;
using Tisbi.LawFirm.DAL.Models;
using Tisbi.LawFirm.UI.Views.Controls;

namespace Tisbi.LawFirm.UI.ViewModel
{
    public class ClaimsCRUDViewModel : BaseCRUDItemViewModel<Claims>
    {
        public RelayCommand SelectCreator { get; private set; }
        public RelayCommand SelectAssingnCustomer { get; private set; }

        public ClaimsCRUDViewModel(Claims model) : base(model)
        {
            SelectCreator = new RelayCommand(SelectCreatorDialog);
            SelectAssingnCustomer = new RelayCommand(SelectAssingnCustomerDialog);
        }

        public DateTime? SelectedDateOfIncoming
        {
            get { return Model.DateOfIncoming; }
            set
            {
                Model.DateOfIncoming = value;
                RaisePropertyChanged(() => SelectedDateOfIncoming);
            }
        }
        public DateTime? SelectedDateOfSending
        {
            get { return Model.DateOfSending; }
            set
            {
                Model.DateOfSending = value;
                RaisePropertyChanged(() => SelectedDateOfSending);
            }
        }
        public DateTime? SelectedDateOfReceip
        {
            get { return Model.DateOfReceip; }
            set
            {
                Model.DateOfSending = value;
                RaisePropertyChanged(() => SelectedDateOfReceip);
            }
        }
        public DateTime? SelectedDateOfDecision
        {
            get { return Model.DateOfDecision; }
            set
            {
                Model.DateOfDecision = value;
                RaisePropertyChanged(() => SelectedDateOfDecision);
            }
        }

        private ObservableCollection<Customers> _creators;
        public ObservableCollection<Customers> Creators
        {
            get { return _creators; }
            set
            {
                _creators = value;
                if (Model.Creator != null)
                {
                    SelectedCreator = Creators.First(x => x.Id == Model.Creator.Id);
                }
                RaisePropertyChanged(() => Creators);
            }
        }

        public Customers SelectedCreator
        {
            get { return Model.Creator; }
            set
            {
                Model.Creator = value;
                RaisePropertyChanged(() => SelectedCreator);
            }
        }

        private ObservableCollection<Customers> _assingnCustomers;
        public ObservableCollection<Customers> AssingnCustomers
        {
            get { return _assingnCustomers; }
            set
            {
                _assingnCustomers = value;
                if (Model.AssingnCustomers != null)
                {
                    SelectedAssingnCustomers = AssingnCustomers.First(x => x.Id == Model.AssingnCustomers.Id);
                }
                RaisePropertyChanged(() => AssingnCustomers);
            }
        }

        public Customers SelectedAssingnCustomers
        {
            get { return Model.AssingnCustomers; }
            set
            {
                Model.AssingnCustomers = value;
                RaisePropertyChanged(() => SelectedAssingnCustomers);
            }
        }

        private void SelectCreatorDialog()
        {
            Customers selectedItem;
            var items = CreateNewDependencyItemDialog<Customers, CustomersListView>("Новое лицо", out selectedItem);
            Creators = items;
            SelectedCreator = selectedItem;
        }

        private void SelectAssingnCustomerDialog()
        {
            Customers selectedItem;
            var items = CreateNewDependencyItemDialog<Customers, CustomersListView>("Новое лицо", out selectedItem);
            AssingnCustomers = items;
            SelectedAssingnCustomers = selectedItem;
        }

        public override string Error
        {
            get { return ""; }
        }

        public override string this[string columnName]
        {
            get { throw new NotImplementedException(); }
        }
    }
}
